package com.ruoyi.course.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.course.domain.CdStudentTopic;
import com.ruoyi.course.enums.TopicStatusEnum;
import com.ruoyi.course.service.ICdStudentTeacherCourseService;
import com.ruoyi.course.service.ICdStudentTopicService;
import com.ruoyi.course.utils.RoleUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.course.mapper.CdTopicMessageMapper;
import com.ruoyi.course.domain.CdTopicMessage;
import com.ruoyi.course.service.ICdTopicMessageService;

/**
 * 题目信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-12-18
 */
@Service
@Slf4j
public class CdTopicMessageServiceImpl implements ICdTopicMessageService 
{
    @Autowired
    private CdTopicMessageMapper cdTopicMessageMapper;

    @Autowired
    private RoleUtils roleUtils;

    @Autowired
    private ICdStudentTopicService cdStudentTopicService;

    @Autowired
    private ICdStudentTeacherCourseService cdStudentTeacherCourseService;

    /**
     * 查询题目信息
     * 
     * @param topicId 题目信息主键
     * @return 题目信息
     */
    @Override
    public CdTopicMessage selectCdTopicMessageByTopicId(Long topicId)
    {
        return cdTopicMessageMapper.selectCdTopicMessageByTopicId(topicId);
    }

    /**
     * 查询题目信息列表
     * 
     * @param cdTopicMessage 题目信息
     * @return 题目信息
     */
    @Override
    public List<CdTopicMessage> selectCdTopicMessageList(CdTopicMessage cdTopicMessage)
    {
        // 普通教师和学生只能查看自己课程下的题目
        if (!roleUtils.isDirector() && roleUtils.isTeacher()) {
            cdTopicMessage.setTeacherId(SecurityUtils.getUserId());
        } else if (roleUtils.isStudent()) {
            List<CdTopicMessage> resultList = new ArrayList<>();

            // 自己已经选择的任何状态的题目
            List<CdStudentTopic> list = cdStudentTopicService.selectCdStudentTopicByStudentId(SecurityUtils.getUserId());
            for (CdStudentTopic cdStudentTopic : list) {
                Long topicId = cdStudentTopic.getTopicId();
                CdTopicMessage topicMessage = cdTopicMessageMapper.selectCdTopicMessageByTopicId(topicId);
                resultList.add(topicMessage);
            }

            // 课程下所有状态为通过学院审核的题目（可选的）
            Set<Long> courseIds = cdStudentTeacherCourseService.listByStudentSelf(SecurityUtils.getUserId());
            for (Long courseId : courseIds) {
                CdTopicMessage topicMessage = new CdTopicMessage();
                topicMessage.setCourseId(courseId);
                topicMessage.setStatus(TopicStatusEnum.TOPIC_APPROVED.getStatus());
                List<CdTopicMessage> messages = cdTopicMessageMapper.selectCdTopicMessageList(topicMessage);

                // 两个范围取并集，并去重
                resultList.removeAll(messages);
                resultList.addAll(messages);
            }

            return resultList;
        }
        return cdTopicMessageMapper.selectCdTopicMessageList(cdTopicMessage);
    }

    /**
     * 新增题目信息
     * 
     * @param cdTopicMessage 题目信息
     * @return 结果
     */
    @Override
    public int insertCdTopicMessage(CdTopicMessage cdTopicMessage)
    {
        // 根据课程和题目名称进行去重
        CdTopicMessage entity = new CdTopicMessage();
        entity.setCourseId(cdTopicMessage.getCourseId());
        entity.setTopicName(cdTopicMessage.getTopicName());
        List<CdTopicMessage> messages = cdTopicMessageMapper.selectCdTopicMessageList(entity);
        if (!messages.isEmpty()) {
            log.error("名称重复");
            return -1;
        }

        cdTopicMessage.setTopicId(System.currentTimeMillis());
        cdTopicMessage.setTeacherId(SecurityUtils.getUserId());
        cdTopicMessage.setStatus(TopicStatusEnum.TOPIC_DRAFT.getStatus()); // 刚添加时为草稿状态
        cdTopicMessage.setCreateBy(SecurityUtils.getUsername());
        cdTopicMessage.setCreateTime(DateUtils.getNowDate());
        return cdTopicMessageMapper.insertCdTopicMessage(cdTopicMessage);
    }

    /**
     * 修改题目信息
     * 
     * @param cdTopicMessage 题目信息
     * @param status 状态
     * @return 结果
     */
    @Override
    public int updateCdTopicMessage(CdTopicMessage cdTopicMessage, String status)
    {
        Long topicId = cdTopicMessage.getTopicId();
        CdTopicMessage topicMessage = cdTopicMessageMapper.selectCdTopicMessageByTopicId(topicId);
        if (!status.equals(topicMessage.getStatus())) {
            log.error("状态不对，无法修改");
            return -1;
        }
        cdTopicMessage.setUpdateBy(SecurityUtils.getUsername());
        cdTopicMessage.setUpdateTime(DateUtils.getNowDate());
        return cdTopicMessageMapper.updateCdTopicMessage(cdTopicMessage);
    }

    /**
     * 修改题目信息
     * @param cdTopicMessage 题目信息
     * @return
     */
    @Override
    public int updateCdTopicMessage(CdTopicMessage cdTopicMessage) {
        return cdTopicMessageMapper.updateCdTopicMessage(cdTopicMessage);
    }

    /**
     * 批量删除题目信息
     * 
     * @param topicIds 需要删除的题目信息主键
     * @return 结果
     */
    @Override
    public int deleteCdTopicMessageByTopicIds(Long[] topicIds)
    {
        return cdTopicMessageMapper.deleteCdTopicMessageByTopicIds(topicIds);
    }

    /**
     * 删除题目信息信息
     * 
     * @param topicId 题目信息主键
     * @return 结果
     */
    @Override
    public int deleteCdTopicMessageByTopicId(Long topicId)
    {
        return cdTopicMessageMapper.deleteCdTopicMessageByTopicId(topicId);
    }
}
